<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag v-model="model.type" placeholder="请选择类型" dictCode="doc_type"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="模板编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入模板编码" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="操作说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operate">
              <a-textarea v-model="model.operate" placeholder="请输入操作说明" :rows="4" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注" :rows="4" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
              <a-form-model-item label="文件上传" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fileList">
                <j-upload ref="upload" v-model="model.fileList"></j-upload>
              </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>

  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'

export default {
  name: 'VcaFileTemplateForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      model:{
        fileList:'',
        fileUrlList:[]
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          { required: true, message: '请输入名称!'},
        ],
        type: [
          { required: true, message: '请输入类型!'},
        ],
        code: [
          { required: true, message: '请输入编码!'},
        ],
        fileList: [
          { required: true, message: '请上传文件!'},
        ]
      },
      url: {
        add: "/villageCadreAssist/vcaFileTemplate/add",
        edit: "/villageCadreAssist/vcaFileTemplate/edit",
        queryById: "/villageCadreAssist/vcaFileTemplate/queryById",
        queryVcaFileItemByMainId: "/villageCadreAssist/vcaFileTemplate/queryVcaFileItemByMainId",
      }
    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  created () {

    //备份model原始值
    // this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add () {
      //this.edit(this.modelDefault);
      this.visible = true;
    },
    edit (record)  {
      this.model = Object.assign({},record);
      // getAction(this.url.queryVcaFileItemByMainId, {id:record["id"]}).then((res)=>{
      //   if(res.success){
      //     res.result.forEach(item=>{
      //       this.$refs.upload.defaultFileList.push(item["fileUrl"]);
      //     })
      //   }else{
      //     this.$message.warning(res.message);
      //   }
      // });
      this.visible = true;
    },
    beforeSubmit(formData) {
      let param = Object.assign({}, formData);
      param["vcaFileItemList"]=[];
      param["fileList"].split(",").forEach(item=>{
        param["vcaFileItemList"].push({
          fileUrl:item
        });
      });
      return param;
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.model.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          let param = this.beforeSubmit(this.model);


          httpAction(httpurl,param,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>

<style scoped>
</style>